<template>
  <Button
    class="action-button action"
    :loading="loading"
    :disabled="!actionEnable"
    v-show="buttonShow"
    :icon="actionModel.icon"
    :type="buttonType"
    @click="actionTrigger"
  >
    {{actionModel.name}}
    </Button>
</template>

<script>
import $ from "@/common";
export default {
  props: {
    model: {}
  },
  data() {
    return {
      actionModel: {},
      actionProto: {},
      //缓存数据
      storageData: null,
      actionData: {},
      loading: false,
      enableStorage: false,
      //强制控制显示状态
      actionHidden: false,
      actionDataIndex: null,
      buttonType: "ghost",
      actionOption: {}
    };
  },
  created() {
    this.$owner = this.$parent.$parent;
    if (!this.$owner.getSelectedDataRow) {
      this.$owner = this.$owner.$parent;
    }
    this.$actionPanel = this.$parent;
    this.$inlineMirror = [];
    this.$set(this, "actionModel", this.baseModelParse());
    if (this.$owner.setAction) {
      this.$owner.setAction(this.actionModel);
    }
    //临时方案，切入actionTrigger设置actionData
    var _trigger = this.actionTrigger;
    this.actionTrigger = (id, data, index, cb, option, ...arg) => {
      if (this.$owner.getSelectedDataRow()) {
        this.actionData = this.$owner.getSelectedDataRow();
      }
      if (this.model.scope === "data" && this.model.locate !== "inline") {
        var ids = this.$owner.getSelectedIds && this.$owner.getSelectedIds();
        if (!data && ids && ids.length > 1) {
          this.$Message.error(`请选择单一数据进行 ${this.model.name} 操作`);
          return;
        }
      }
      if (
        (index === undefined || index === null) &&
        (this.model.scope === "data" || this.model.scope === "rows") &&
        this.$owner.getSelectedIndex
      ) {
        index = this.$owner.getSelectedIndex();
      }
      if (option && option.enable_storage) {
        this.enableStorage = true;
        this.storageData = option.storage;
      } else {
        this.enableStorage = false;
        this.storageData = null;
      }
      return _trigger.call(this, id, data, index, cb, option, ...arg);
    };
  },
  mounted() {
    this.$emit("loaded");
    this.$owner.$emit("action-loaded:" + this.actionModel.code);
  },
  computed: {
    buttonShow() {
      return this.actionModel.locate !== "inline" && !this.actionHidden;
    },
    actionEnable() {
      var scope = this.actionModel ? this.actionModel.scope : null;
      if (scope === "rows" || scope === "line" || scope === "data") {
        return this.$parent.selectStatus;
      } else {
        return true;
      }
    },
    successText() {
      return this.actionModel.name + "操作成功";
    }
  },
  methods: {
    //动作触发，一般为点击按钮或通过store action进行触发
    actionTrigger() {},
    //动作执行 开始执行动作
    actionStart() {},
    //进行提交
    actionSubmit() {},
    clearStorage() {
      this.$actionPanel.$emit("storageClear", {
        code: this.model.code,
        model: this.model,
        storage: this.storageData
      });
      this.$set(this, "storageData", null);
    },
    actionSuccess(text) {
      if (text && text.data) text = text.data;
      this.$Notice.success({
        title: text || this.successText
      });
      this.$actionPanel.$emit("actionSuccess", {
        code: this.model.code,
        model: this.model
      });
    },
    getSubmitData() {
      var data = {};
      switch (this.actionModel.scope) {
        case "rows":
          //获取父级数据控件选中的id
          data = this.$owner.getSelectedIds();
          break;
        case "data":
          data = this.$owner.getSelectedDataRow();
          break;
        case "line":
          data = this.$owner.getSelectedId();
          break;
        case "optional":
          data.rows = this.$owner.getSelectedIds();
          data.param = this.$owner.filterNow || {};
          break;
      }
      return data;
    },
    getDataRow() {
      return this.$owner.getSelectedDataRow();
    },
    modelParam(code) {
      if (!code) return null;
      var arr = code.split(".");
      var node = this.actionModel.param;
      for (let i in arr) {
        if (node === null || node === undefined) return null;
        node = node[arr[i]];
      }
      return node;
    },
    //action 解析公共处理 一般不进行覆盖
    baseModelParse() {
      var m = $.copyJson(this.model);
      //通过model的原型属性为model设置默认属性
      if (this.actionProto) {
        m = $.setObj(m, this.actionProto);
      }
      if (!m.param) m.param = {};
      return this.modelParse(m);
    },
    //普通model解析 用于覆盖
    modelParse(m) {
      return m;
    }
  }
};
</script>

<style lang="less">
.action-group {
  display: inline-block;
  margin-right: 4px;
  .select-box {
    display: inline-block;
    width: 120px;
  }
  .ivu-select-selection {
    border-radius: 4px 0 0 4px;
  }
  .select-box + .ivu-btn {
    margin-left: -5px;
    border-radius: 0 4px 4px 0;
  }
  .radio-box {
    display: inline-block;
    padding: 5px 8px 6px 16px;
    border-radius: 4px 0 0 4px;
    border: 1px solid #dddee1;
  }
  .radio-box + .ivu-btn {
    margin-left: -5px;
    border-radius: 0 4px 4px 0;
  }
}
</style>
